-- SPDX-License-Identifier: GPL-3.0-or-later
---@class StoryData: Object
local Story = class("Story")
local Map = require "packages/wdtx/Map"
Story.TypeStoryEnd = "storyend"
Story.TypeNormal = "normal"
Story.TypeChoices = "choices"
Story.TypeFight = "fight"

Story.ChoiceNormal = "choicenormal"
Story.ChoiceEvent = "choiceevent"
Story.ChoiceBranchEvent = "choicebranchevent"

Story.TalkLeftPos = 1
Story.TalkRightPos = 1
Story.ContinueStory = "继续"
Story.FightStory = "战斗"
Story.NextStory = "下一章"
-- Story.MapChoices= {
--   normonster = "战斗",
--   elitemonster = "战斗",
--   box = "打开看看",
--   boss = "战斗",
--   shop = "进去看看",
--   otherevents = "过去试试",
--   rest = "休息一下"
-- }

Story.StatusNormal = 0
Story.StatusDeath = 1
Story.StatusEnd = 2
Story.StatusContinue = 3
Story.StatusObserve = 4
Story.StatusChoice = 5
Story.StatusFight = 6

function Story:initialize(player,data,obj)
  self.player = player
  self.wdtxobj = obj
  self.id = 1
  self.dialogconfig = data.dialogconfig
  self.events = data.events
  self.status = 0
  self.eventlen = 0
  self.curinfo = {}
  self.dialogdata = {
    normonster = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方发现普通怪物"}},
        choices = {"战斗"}, 
        eventchc = {1}
      }
    },
    elitemonster = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方发现精英怪物"}},
        choices = {"战斗"}, 
        eventchc = {1}
      }
    },
    boss = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方发现BOSS"}},
        choices = {"战斗"}, 
        eventchc = {1}
      }
    },
    box = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方发现宝箱"}},
        choices = {"打开"}, 
        eventchc = {1}
      }
    },
    shop = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方发现商店"}},
        choices = {"进入"}, 
        eventchc = {1}
      }
    },
    otherevents = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方有情况。"}},
        choices = {"去看看"}, 
        eventchc = {1}
      }
    },
    rest = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方有个休息地方"}},
        choices = {"休息一下"}, 
        eventchc = {1}
      }
    },
    addmaxhp = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="找到了💖，体力上限+1"}},
        choices = {"注入灵魂"}, 
        eventchc = {1}
      }
    },
    ["getcd-wdtx__peach"] = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方有张卡牌【桃】"}},
        choices = {"获得","离开"}, 
        eventchc = {1,1}
      }
    },
    ["getcd-wdtx__analeptic"] = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方有张卡牌【酒】"}},
        choices = {"获得","离开"}, 
        eventchc = {1,1}
      }
    },
    ["getcd-wdtx__slash"] = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方有张卡牌【杀】"}},
        choices = {"获得","离开"}, 
        eventchc = {1,1}
      }
    },
    ["getcd-wdtx__archery_attack"] = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方有张卡牌【万箭齐发】"}},
        choices = {"获得","离开"}, 
        eventchc = {1,1}
      }
    },
    ["getcd-wdtx__savage_assault"] = {
      {dialog = {{talkers={1,self.player.general,2,"hxfc_author"},say="前方有张卡牌【南蛮入侵】"}},
        choices = {"获得","离开"}, 
        eventchc = {1,1}
      }
    },
  }
end

function Story:resetProgress()
  self.eventlen = 0
end

function Story:getCurProgress()
  return self.eventlen / Map.ColumnDeep
end

function Story:getEvent()
  return self.events[1]
end

function Story:getDialog()
  local dialogcfg = self.dialogconfig[1] 
  return dialogcfg or nil
end

function Story:addEvents(evts)
  table.insertTable(self.events,evts)
end

------------可以自动调节剧情事件栈-----------
function Story:addEvent(evtname,pos)
  table.insert(self.events,pos,evtname)
end
function Story:addDialog(dialog,pos)
  table.insert(self.dialogconfig,pos,dialog)
end
function Story:addNewStory(evtname,dialog,pos)
  -- p(evtname)
  -- p(dialog)
  -- p(pos)
  -- dbg()
  self:addEvent(evtname,pos)
  self:addDialog(dialog,pos)
  -- p(self.events)
  -- p(self.dialogconfig[1])
  -- dbg()
end

function Story:addSubDialog(subdialog,rootpos,subpos)
  table.insert(self.dialogconfig[rootpos],subpos,subdialog)
end
------------可以自动调节剧情事件栈-----------

function Story:setID(id)
  self.id = id
end

function Story:getID()
  return self.id
end

function Story:setStatus(status)
  self.status = status
end

function Story:getStatus()
  return self.status
end

function Story:getDioalogData()
  
end

--刷新剧情内容
function Story:refreshStory(evts)
  self:addEvents(evts)
  self.eventlen = self.eventlen + 1
  
  for i,evt in ipairs(self.events) do
    if not self.dialogconfig[i] then
      local data = table.clone(self.dialogdata[evt])
      if string.startsWith(evt,"getcd") then
        local cname = evt:split("-")        
        cname = cname[#cname]
        local cards = self.wdtxobj:findCards({[cname] = 1},self.player.room.void)
        local upcard = Fk:getCardById(cards[1])
        self.wdtxobj:upCardMark(upcard)
        data[1].cid = cards[1]
      end
      table.insert(self.dialogconfig,data)
    end    
  end
  -- p(self.events)
  -- p(self.dialogconfig)
  -- dbg()
end
--移除当前剧情
function Story:removeStory()
  table.remove(self.dialogconfig,1)
  local curevent = table.remove(self.events,1)
  if curevent == "boss" then
    self:resetProgress()
  end
end

function Story:setCurInfo(data)
  self.curinfo = data
end

function Story:getCurInfo()
  return self.curinfo
end

return Story
